package com.example.first;

import java.io.File;
import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

public class AudioRecorder {

    private MediaRecorder recorder = new MediaRecorder();
    private String path;
    private String outfile = null;

    public AudioRecorder(){}

    public void startRecording(String audioFile) throws IOException {
        String state = android.os.Environment.getExternalStorageState();
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        // make sure the directory we plan to store the recording in exists
//
//
    File FILE  = new File( Environment.getExternalStorageDirectory() +"/mic2.3gpp");
     //   if (!directory.exists() && !directory.mkdirs()) {
    //        throw new IOException("Path to file could not be created.");
       // }

            //File storageDir = new File(Environment
              //      .getExternalStorageDirectory(), "/");
            //storageDir.mkdir();
            //outfile=File.createTempFile(audioFile, ".3gpp",storageDir);
      outfile = Environment.getExternalStorageDirectory() +"/mic2.3gpp";
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            Log.e("wawa" , "path: " + outfile);
            recorder.setOutputFile(outfile);


        try{
            recorder.prepare();
        }catch(IllegalStateException e){
            e.printStackTrace();
        }

        recorder.start();
    }

    public String stop() throws IOException {
        recorder.stop();
        recorder.release();
        return outfile;
    }
}